package tagging;

import java.util.HashMap;
import java.util.Vector;

public class SuffixHandler {
  private static String[] suffixList_ = {
    "able", "al", "an", "ance", "ant",
    "ar", "ate", "cede", "dom", "duce", "duct", "ed", "ee", "en", "ent",
    "er", "ese", "fect", "fer", "fix", "ful", "fuse", "gest", "hood", "ic",
    "ify", "ing", "ion", "ish", "ity", "ive", "ize", "ject", "ly", "ment",
    "mit", "mote", "ness", "or", "ous", "rive", "s", "sert", "ship", "side",
    "sign", "sist", "sume", "test", "th", "vene",
  };
  
  public static class SuffixTag {
    HashMap<String, Integer> tagCount_;
    
    SuffixTag() {
      tagCount_ = new HashMap<String, Integer>();
    }
    
    void addTag(String tag) {
      Integer tc = tagCount_.get(tag);
      
      if (tc == null) {
        tagCount_.put(tag, 1);
      } else {
        tagCount_.put(tag, tc + 1);
      }
    }
    
    int getTagCount(String tag) {
      Integer tc = tagCount_.get(tag);
      
      if (tc == null) return 0;
      return tc;
    }
  }
  
  HashMap<String, SuffixTag> suffixMap_;
  
  public SuffixHandler() {
    suffixMap_ = new HashMap<String, SuffixHandler.SuffixTag>();
    
    for (int i = 0; i < suffixList_.length; ++i) {
      SuffixTag st = new SuffixTag();
      suffixMap_.put(suffixList_[i], st);
    }
  }

  public void handleWord(String word, String tag) {
    int len = word.length();

    for (int sz = 2; sz <= 4; sz++) {
      if (sz >= len) break;
      
      String suffix = word.substring(len - sz, len);
      
      SuffixTag st = suffixMap_.get(suffix);
      
      if (st != null) {
        st.addTag(tag);
      }
    }
  }
  
  public int getSuffixCount(String word, String tag) {
    int len = word.length();
    
    int maxCount = 0;
    for (int sz = 2; sz <= 4; sz++) {
      if (sz >= len) break;
      
      String suffix = word.substring(len - sz, len);
      
      SuffixTag st = suffixMap_.get(suffix);
      
      if (st != null)
        maxCount = Math.max(maxCount, st.getTagCount(tag));
    }
  
    return maxCount;
  }
}
